<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<#if model??>
<mapper namespace="${model.packageName}.${model.model}<#if model.subModel??>.${model.subModel}</#if>.dao.${model.className}Dao">
    ${"<!-- 公共查询字段 @author " + model.authorNm + "-->"}
    <sql id="${model.className}Columns">
        <#list model.columns as column>
        <#if !column_has_next>
            a.${column.dbColumnName} as "${column.columnName}"
        <#else>
            a.${column.dbColumnName} as "${column.columnName}",
        </#if>
        </#list>
    </sql>

    ${"<!-- 公共连表查询语句 @author " + model.authorNm + "-->"}
    <sql id="${model.className}Joins">
    </sql>

    ${"<!-- 查询详情 @author " + model.authorNm + "-->"}
    <select id="get" resultType="${model.packageName}.${model.model}<#if model.subModel??>.${model.subModel}</#if>.entity.${model.className}">
        SELECT <include refid="${model.className}Columns"/>
          FROM ${model.tableName} a <include refid="${model.className}Joins"/>
        <where>
        <#list model.columns as column>
        <#if column.key=="PRI">
           AND a.${column.dbColumnName} = ${"#"}{${column.columnName}}
        </#if>
        <#if column.columnName=="status">
           AND a.status = '0'
        </#if>
        </#list>
        </where>
        limit 1
    </select>

    ${"<!-- 查询详情 - 根据参数查询 @author " + model.authorNm + "-->"}
    <select id="getByParam" resultType="${model.packageName}.${model.model}<#if model.subModel??>.${model.subModel}</#if>.entity.${model.className}">
        SELECT <include refid="${model.className}Columns"/>
          FROM ${model.tableName} a <include refid="${model.className}Joins"/>
        <where>
        <#list model.columns as column>
        <#if column.columnName=="status">
                AND a.status = '0'
        <#else >
            <if test=" ${column.columnName} != null and ${column.columnName} != '' ">
                AND a.${column.dbColumnName} = ${"#"}{${column.columnName}}
            </if>
        </#if>
        </#list>
        </where>
        LIMIT 1
    </select>

    ${"<!-- 获取list列表数据 @author " + model.authorNm + "-->"}
    <select id="getList" resultType="${model.packageName}.${model.model}<#if model.subModel??>.${model.subModel}</#if>.entity.${model.className}">
        SELECT <include refid="${model.className}Columns"/>
          FROM ${model.tableName} a
        <include refid="${model.className}Joins"/>
        <where>
        <#list model.columns as column>
            <#if column.columnName=="status">
                AND a.status = '0'
            <#else >
            <if test=" ${column.columnName} != null and ${column.columnName} != '' ">
                AND a.${column.dbColumnName} = ${"#"}{${column.columnName}}
            </if>
            </#if>
        </#list>
        </where>
    </select>

    ${"<!-- 获取分页数据 @author " + model.authorNm + "-->"}
    <select id="getPage" resultType="${model.packageName}.${model.model}<#if model.subModel??>.${model.subModel}</#if>.entity.${model.className}">
        SELECT <include refid="${model.className}Columns"/>
          FROM ${model.tableName} a
        <include refid="${model.className}Joins"/>
        <where>
        <#list model.columns as column>
            <#if column.columnName=="status">
                AND a.status = '0'
            <#else >
            <if test=" entity.${column.columnName} != null and entity.${column.columnName} != '' ">
                AND a.${column.dbColumnName} = ${"#"}{entity.${column.columnName}}
            </if>
            </#if>
        </#list>
        </where>
        <if test="orderBy != null and orderBy!=''">
            ORDER BY ${"$"}{ orderBy}
        </if>
    </select>

    ${"<!-- 新增插入 @author " + model.authorNm + "-->"}
    <insert id="insert" useGeneratedKeys="true" keyProperty="id" >
        INSERT INTO ${model.tableName}(
        <#list model.columns as column>
            <#if column.key=="PRI" && column.columnName=="id">
            <#else>
                <#if !column_has_next>
                ${column.dbColumnName}
                <#else>
                ${column.dbColumnName},
                </#if>
            </#if>
        </#list>
        )
        VALUES (
        <#list model.columns as column>
            <#if column.key=="PRI" && column.columnName=="id">
            <#else>
                <#if !column_has_next>
                ${"#"}{${column.columnName}}
                <#else>
                ${"#"}{${column.columnName}},
                </#if>
            </#if>
        </#list>
        )
    </insert>

    ${"<!-- 更新数据 @author " + model.authorNm + "-->"}
    <update id="update">
        UPDATE ${model.tableName} SET
        <#list model.columns as column>
            <#if column.key=="PRI" && column.columnName=="id">
            <#else>
                <#if !column_has_next>
               ${column.dbColumnName} = ${"#"}{${column.columnName}}
                <#else>
               ${column.dbColumnName} = ${"#"}{${column.columnName}},
                </#if>
            </#if>
        </#list>
        <where>
            <#list model.columns as column>
            <#if column.key=="PRI" && column.columnName=="id">
           AND ${column.dbColumnName} = ${"#"}{${column.columnName}}
            </#if>
            </#list>
        </where>
    </update>

    ${"<!-- 删除 根据主键删除 @author " + model.authorNm + "-->"}
    <delete id="delete">
        DELETE FROM ${model.tableName}
        <where>
            <#list model.columns as column>
            <#if column.key=="PRI">
                AND ${column.dbColumnName} = ${"#"}{${column.columnName}}
            </#if>
            </#list>
        </where>
    </delete>

    ${"<!-- 删除 根据其他参数删除,切记，此方法慎用，稍有不慎就会删除所有数据 @author " + model.authorNm + "-->"}
    <delete id="deleteByParam">
        DELETE FROM ${model.tableName}
        <where>
            <#list model.columns as column>
            <if test=" ${column.columnName} != null and ${column.columnName} != '' ">
                AND ${column.dbColumnName} = ${"#"}{${column.columnName}}
            </if>
            </#list>
        </where>
    </delete>

    ${"<!-- 逻辑删除 根据主键删除 @author " + model.authorNm + "-->"}
    <delete id="deleteLogic">
        UPDATE ${model.tableName} SET status = '1'
        <where>
        <#list model.columns as column>
            <#if column.key=="PRI">
            AND ${column.dbColumnName} = ${"#"}{${column.columnName}}
            </#if>
        </#list>
        </where>
    </delete>

    ${"<!-- 逻辑删除 根据其他参数删除,切记，此方法慎用，稍有不慎就会删除所有数据 @author " + model.authorNm + "-->"}
    <delete id="deleteLogicByParam">
        UPDATE ${model.tableName} SET status = '1'
        <where>
            <#list model.columns as column>
            <if test=" ${column.columnName} != null and ${column.columnName} != '' ">
                AND ${column.dbColumnName} = ${"#"}{${column.columnName}}
            </if>
            </#list>
        </where>
    </delete>
</mapper>
</#if>